<?php
use_stylesheet('list.css');
use_helper('Text');
$kpidaysRaw=$sf_data->getRaw("kpidays");

$start = new DateTime($start->format("y-m-d"));
$end = new DateTime($end->format("y-m-d"));
$end = $end->modify( '+1 day' ); 
$interval = new DateInterval('P1D');

$period=new DatePeriod($start, $interval, $end);
$days=array();
foreach ($period as $day)
    $days[]=$day;
$days= array_reverse($days);

//var_dump($kpidays);die;
?>


<table border="1" id="lista">
    <thead>
        <tr>
            <th>Plant</th>
            <?php
            foreach ($days as $day): ?>
            <th><?php echo $day->format("d/m")?></th>
      <?php endforeach;?>  
        </tr>
    </thead>
    <tbody>
    <?php
        $plant="";
        $prevDay=$start;
        
        foreach ($kpidaysRaw as $plantId=>$plantKpiday){
            
            if ($prevDay<>$start){     
                //some days are missing after the last value: the row is filled with "NA"
                for ($i=1;$i<=$prevDay->diff($start, TRUE)->days;$i++){
                ?>  <td class="highVis"> NA </td>
     <?php      }
            }
            
            $odd="even";
            $plant=Doctrine_Core::getTable('plant')->find($plantId);
            $prevDay=$end;?>
            <tr class="<?php echo $odd ?>">
            <td><a href="<?php echo url_for('kpiday_show_plant',$plant) ?>"><?php echo $plant->getName() ?></a></td>
     
    <?php   if ( !isset($plantKpiday)){
                // no data for this plant
                    $prevDay=$start;  ?>
                    <td class="highVis" colspan="<?php echo count($days)?>"> NA </td>
    <?php   }else{
                foreach($plantKpiday as $data){
                    if (isset($data["subplant"])) continue;
                    $dayString=$data["day"];
                    $day=new datetime($dayString);
                    if ($day->diff($prevDay, TRUE)->days>1){
                    // there are some days missing. The cells are filled with NA
                        for ($i=1;$i<$day->diff($prevDay, TRUE)->days;$i++){
                            ?> <td class="redlight" > NA </td>
     <?php              }  
                    } 
                    // print the day value?>         
                    <td><?php echo $data["pr"]*100 ?></td>
     <?php  
                    $prevDay=new DateTime($dayString);
                }
            }   
        } ?>      
    </tbody>
</table>